package org.laobai.shop.mgt.controller;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.FileOutputStream;
import java.io.IOException;

@Controller
@RequestMapping("/")
public class IndexController {

    @RequestMapping("/")
    public String index(){
        return "index";
    }


    public static void main(String[] args) throws IOException, DocumentException {
        /*
        1.新建document对象，可通过一下三种任意一种
        2.建立一个书写器(Writer)与document对象关联，通过书写器(Writer)可以将文档写入到磁盘中。
        3.打开文档
        4.向文档中添加内容
        5.关闭文档
         */
        Document document = new Document(PageSize.A4);
        PdfWriter pdfWriter = PdfWriter.getInstance(document, new FileOutputStream("D://test//1.pdf"));

        document.open();
        // 追加内容

        // 设置字体
        // 字体名
        // UniGB-UCS2-H UniGB-UCS2-W
        BaseFont font = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

        // 添加段落
        Paragraph paragraph = new Paragraph("这是一个demo", new Font(font));
        paragraph.setSpacingAfter(10f); //设置段落下空白
        document.add(paragraph);

        PdfPTable pdfPTable = new PdfPTable(3);

        pdfPTable.addCell(new PdfPCell(new Phrase("ID",new Font(font))));
        pdfPTable.addCell(new PdfPCell(new Phrase("品牌",new Font(font))));
        pdfPTable.addCell(new PdfPCell(new Phrase("首字母",new Font(font))));
        pdfPTable.addCell(new PdfPCell(new Phrase("1",new Font(font))));
        pdfPTable.addCell(new PdfPCell(new Phrase("小米",new Font(font))));
        pdfPTable.addCell(new PdfPCell(new Phrase("X",new Font(font))));
        pdfPTable.addCell(new PdfPCell(new Phrase("2",new Font(font))));
        pdfPTable.addCell(new PdfPCell(new Phrase("华为",new Font(font))));
        pdfPTable.addCell(new PdfPCell(new Phrase("H",new Font(font))));
        document.add(pdfPTable);

//        // 添加表格
//        PdfPTable table = new PdfPTable(3);
//        table.addCell(new PdfPCell(new Phrase("姓名",new Font(font))));
//        table.addCell(new PdfPCell(new Phrase("性别",new Font(font))));
//        table.addCell(new PdfPCell(new Phrase("年龄",new Font(font))));
//        table.addCell(new PdfPCell(new Phrase("张三",new Font(font))));
//        table.addCell(new PdfPCell(new Phrase("男",new Font(font))));
//        table.addCell(new PdfPCell(new Phrase("23",new Font(font))));
//        table.addCell(new PdfPCell(new Phrase("李四",new Font(font))));
//        table.addCell(new PdfPCell(new Phrase("女",new Font(font))));
//        table.addCell(new PdfPCell(new Phrase("18",new Font(font))));
//
//        table.setSpacingAfter(10f);
//        document.add(table);
//
        // 添加图片
        Image image = Image.getInstance("D://test//imgs//anan.png");
        image.setAlignment(Image.ALIGN_CENTER);
        image.scalePercent(40);//依照比例缩放
        document.add(image);

        document.close();

    }
}
